package com.bruce.algorithm.leetcode;

/**
 * @author luotuan
 * @Description 509. 斐波那契数
 * @create 2020-07-14 17:19
 **/
public class Fib {
    public static int fib(int n) {
        if (n <= 1) return n;
        int first = 0;
        int second = 1;
        while (n-- > 1) {
            second = first + second;
            first = second - first;
        }
        return second;
    }

    public static int fib2(int n) {
        double c = Math.sqrt(5);
        return (int) ((Math.pow((1 + c) / 2, n) - (Math.pow((1 - c) / 2, n)))  / c);
    }

    public static void main(String[] args) {
        System.out.println(fib2(4));
    }
}
